package com.example.mpandroidchart1107;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;

import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;

import java.util.ArrayList;
import java.util.List;

public class Main2Activity extends AppCompatActivity {
  //  private BarChart barChart;
    /*模板地址
    https://blog.csdn.net/weixin_43344890/article/details/103007183*/
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bar_chart);
        setTitle("柱形图啊啊啊啊");
        /*方法2*/
        BarChart barChart=findViewById(R.id.barchart);
        List<IBarDataSet> s=new ArrayList<>();

        List<BarEntry> bar1 = new ArrayList<>();
        bar1.add(new BarEntry(0, 37f));
        bar1.add(new BarEntry(1, 56f));
        bar1.add(new BarEntry(2, 64f));
        bar1.add(new BarEntry(3, 38f));
        bar1.add(new BarEntry(4, 59f));
        BarDataSet barDataSet1 = new BarDataSet(bar1, "01");
        barDataSet1.setValueTextColor(Color.BLUE); // 数值的颜色
        barDataSet1.setValueTextSize(10f); // 数值的大小
        barDataSet1.setColor(Color.parseColor("#7EC0EE"));//柱子颜色
        // 柱子的颜色
        barDataSet1.setLabel("苹果");
        // 设置标签之后，图例的内容默认会以设置的标签显示
        // 设置柱子上数据显示的格式
        s.add(barDataSet1);

        List<BarEntry> bar2 = new ArrayList<>();
        bar2.add(new BarEntry(0.3f, 20f));
        bar2.add(new BarEntry(1.3f, 48f));
        bar2.add(new BarEntry(2.3f, 30f));
        bar2.add(new BarEntry(3.3f, 40f));
        bar2.add(new BarEntry(4.3f, 25f));
        BarDataSet barDataSet2 = new BarDataSet(bar2, "02");
        barDataSet2.setValueTextColor(Color.BLUE); // 数值的颜色
        barDataSet2.setValueTextSize(10f); // 数值的大小
        barDataSet2.setColor(Color.parseColor("#FFAEB9"));//柱子颜色
        barDataSet2.setLabel("栗子");
        s.add(barDataSet2);


        BarData barData = new BarData(s);
        barData.setBarWidth(0.3f); // 设置柱子的宽度
        barChart.setData(barData);
    }

    private BarDataSet getBarDataSet(float[] f, String title){
        List<BarEntry> data = new ArrayList<>();
        for(int x = 0; x < f.length; x++){
            BarEntry barEntry = new BarEntry(x, f[x]);
            data.add(barEntry);
        }
        return new BarDataSet(data, title);
    }

}
                            /*方法01*/
//    BarChart barChart=findViewById(R.id.barchart);//实例化
//    List<IBarDataSet> s=new ArrayList<>();/*调用IBarDataSet方法*/
//
//    float[] f1={22f,33f,13f,34f,55f};
//    BarDataSet bar1 =getBarDataSet(f1,"柱2");
//        bar1.setColor(Color.parseColor("#00BFFF"));
//        bar1.setValueTextSize(10f); // 数值显示的的大小
//        s.add(bar1);
//
//    float[] f2={11f,17f,28f,43f,32f};
//    BarDataSet bar2 =getBarDataSet(f2,"柱形图2");
//       bar2.setColor(Color.parseColor("#90EE90"));
//        bar2.setBarBorderWidth(0.5f);////柱状数据的大小
//        bar2.setValueTextSize(10f); // 数值显示的大小
//        s.add(bar2);
//          barChart.getDescription().setEnabled(false); // 不显示描述
//        barChart.setExtraOffsets(20, 20, 20, 20);
//    // 设置饼图的偏移量，类似于内边距 ，设置视图窗口大小
//
//    BarData barData=new BarData(s);
//
//        barData.setBarWidth(0.4f);//设置柱子的宽度是
//        barData.groupBars(0.1f,0.1f,0.05f);
//        barChart.setData( barData);
//        barChart.invalidate();
//
//}